package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentExample;
import com.dongdongshop.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Map;

@Service
@org.springframework.stereotype.Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContent> contentList() {
        TbContentExample example = new TbContentExample();
        example.setOrderByClause("sort_order desc");
        return tbContentMapper.selectByExample(example);
    }

    @Override
    public Boolean save(TbContent tbContent) {
        try {
            Integer i =tbContentMapper.insert(tbContent);
            return i>0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public TbContent getById(long id) {
        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer contentUpdate(TbContent tbContent) {
        return tbContentMapper.updateByPrimaryKey(tbContent);
    }

    @Override
    public Integer deleteContent(Long[] id) {
        return tbContentMapper.deleteContent(id);
    }

    @Override
    public List<TbContent> getContent(Long categoryId, Integer pageSize) {
        List<TbContent> content = tbContentMapper.getContent(categoryId, pageSize);
        String key = "reids:ContentInfo";
        String feild = "feildContent"+categoryId;
        List<TbContent> treeList=(List<TbContent>) redisTemplate.boundHashOps(key).get(key);
        if (treeList != null && !treeList.isEmpty()){
            return treeList;
        }
        treeList = content;
        redisTemplate.boundHashOps(key).put(feild,treeList);

        return treeList;
    }
}
